'use strict'

define(['jquery', 'bootstrap', 'bootstrapTable', 'bootstrapTableCN', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.date, data.site, data.period, data.today);
        this.exportReport();
    },
    
    initPage: function(date, site, period, today){
    	$('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });
    	
    	$('select[name="detailView"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });

          $('select[name="site"]').change(function(){ 

            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/finished-course-reports?site=" + site + "&date=" + date + "&period=" + period;

          })
          
          $('.date').datetimepicker({
              format: 'yyyy-mm-dd',
              autoclose: true,
              language: 'zh-CN',
              todayBtn: 'linked',
              todayHighlight: true,
              pickerPosition: 'bottom-right',
              minView: 'month',
              startDate: '2017-05-01',
              endDate: today
            });
          
          $('#grid-wrap').on("click", ".pagination a", function(event){
              event.preventDefault();
              $.ajax({
                url: $(this).attr("href"),
                type : "GET"
              }).done(function( data ) {
            	  $("#grid-wrap").empty().append(data);
              });
            })

            // init grid wrap
            var $detailView = $('select[name="detailView"]'), url;
            $.ajax({
              url: window.__CTX__.root + '/finished-course-reports/' + $detailView.val() + '-grid?date=' + date + "&site=" + site + "&period=" + period,
              type: "GET"
            }).done(function( data ) {
            	$("#grid-wrap").empty().append(data);
            });
          
          $('input[name="date"]').change(function() {
        	  window.location.href = window.__CTX__.root + "/finished-course-reports?date=" + $(this).val() + "&site=" + site + "&period=" + period;
          })
          
          $detailView.change(function(){
        	  $.ajax({
              url: window.__CTX__.root + '/finished-course-reports/' + $detailView.val() + '-grid?date=' + date + "&site=" + site + "&period=" + period,
              type: "GET"
            }).done(function( data ) {
            	$("#grid-wrap").empty().append(data);
            });
          })
    },
    
    exportReport: function() {
        $(document).on('shown.bs.modal', '#report-export', function(e){
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate

            $('button[type="submit"').click(function() {
                $(e.currentTarget).find("form").trigger("validate.ajax.submit");
                $.bt_validate.result[$.bt_validate.form_id]
            });
        });
    }
  }
})
